<template>
  <div class="product-detail">
    <!-- 导航栏 -->
    <van-nav-bar
      title="商品详情"
      left-arrow
      @click-left="goBack"
      fixed
    >
      <template #right>
        <van-icon name="share-o" @click="shareProduct" />
      </template>
    </van-nav-bar>

    <div v-if="product" class="detail-content">
      <!-- 商品图片轮播 -->
      <van-swipe class="product-swipe" :autoplay="3000" indicator-color="white">
        <van-swipe-item>
          <img :src="product.image" alt="商品图片" class="product-image" />
        </van-swipe-item>
        <!-- 可以添加更多图片 -->
        <van-swipe-item>
          <img :src="product.image" alt="商品图片" class="product-image" />
        </van-swipe-item>
      </van-swipe>

      <!-- 商品基本信息 -->
      <div class="product-info">
        <div class="price-section">
          <span class="current-price">¥{{ product.price }}</span>
          <span v-if="product.tag" class="product-tag">{{ product.tag }}</span>
        </div>
        <h1 class="product-title">{{ product.title }}</h1>
        <div class="product-subtitle">{{ product.description }}</div>
        
        <!-- 商品参数 -->
        <div class="product-params">
          <van-cell title="库存" :value="`${product.stock}件`" />
          <van-cell title="销量" value="1000+" />
          <van-cell title="配送" value="免费配送" />
        </div>
      </div>

      <!-- 商品详情 -->
      <div class="detail-section">
        <van-tabs v-model:active="activeTab">
          <van-tab title="商品详情">
            <div class="detail-content-tab">
              <h3>产品特色</h3>
              <p>{{ product.description }}</p>
              
              <h3>产品参数</h3>
              <van-cell-group>
                <van-cell title="品牌" value="Apple" />
                <van-cell title="型号" value="iPhone 15 Pro Max" />
                <van-cell title="颜色" value="深空黑色" />
                <van-cell title="存储容量" value="256GB" />
                <van-cell title="屏幕尺寸" value="6.7英寸" />
              </van-cell-group>

              <h3>产品图片</h3>
              <div class="detail-images">
                <img :src="product.image" alt="详情图片" />
                <img :src="product.image" alt="详情图片" />
              </div>
            </div>
          </van-tab>
          
          <van-tab title="用户评价">
            <div class="reviews-tab">
              <div class="review-summary">
                <div class="rating-score">4.8</div>
                <div class="rating-info">
                  <van-rate v-model="rating" readonly />
                  <div class="review-count">共1024条评价</div>
                </div>
              </div>
              
              <div class="review-list">
                <div v-for="review in reviews" :key="review.id" class="review-item">
                  <div class="review-header">
                    <van-image
                      :src="review.avatar"
                      round
                      width="32"
                      height="32"
                    />
                    <div class="review-user">
                      <div class="username">{{ review.username }}</div>
                      <van-rate v-model="review.rating" readonly size="12" />
                    </div>
                    <div class="review-date">{{ review.date }}</div>
                  </div>
                  <div class="review-content">{{ review.content }}</div>
                </div>
              </div>
            </div>
          </van-tab>
        </van-tabs>
      </div>
    </div>

    <!-- 商品不存在 -->
    <van-empty
      v-else
      description="商品不存在"
      image="search"
    />

    <!-- 底部操作栏 -->
    <div v-if="product" class="action-bar">
      <div class="action-icons">
        <div class="action-item" @click="toggleFavorite">
          <van-icon :name="isFavorite ? 'star' : 'star-o'" :color="isFavorite ? '#ff4444' : '#666'" />
          <span>收藏</span>
        </div>
        <div class="action-item" @click="goToCart">
          <van-icon name="cart-o" />
          <span>购物车</span>
          <van-badge v-if="cartCount > 0" :content="cartCount" />
        </div>
      </div>
      <div class="action-buttons">
        <van-button
          type="warning"
          @click="addToCart"
          class="add-cart-btn"
        >
          加入购物车
        </van-button>
        <van-button
          type="danger"
          @click="buyNow"
          class="buy-now-btn"
        >
          立即购买
        </van-button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useProductStore } from '@/stores/product'
import { useCartStore } from '@/stores/cart'
import { showToast, showSuccessToast } from 'vant'

const router = useRouter()
const route = useRoute()
const productStore = useProductStore()
const cartStore = useCartStore()

const activeTab = ref(0)
const isFavorite = ref(false)
const rating = ref(4.8)

// 获取商品ID
const productId = computed(() => Number(route.params.id))

// 获取商品信息
const product = computed(() => productStore.getProductById(productId.value))

// 购物车商品数量
const cartCount = computed(() => cartStore.totalCount)

// 模拟评价数据
const reviews = ref([
  {
    id: 1,
    username: '用户***123',
    avatar: 'https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?w=60&h=60&fit=crop&crop=face',
    rating: 5,
    date: '2024-01-15',
    content: '商品质量很好，物流很快，包装也很精美，非常满意！'
  },
  {
    id: 2,
    username: '用户***456',
    avatar: 'https://images.unsplash.com/photo-1494790108755-2616b612b786?w=60&h=60&fit=crop&crop=face',
    rating: 4,
    date: '2024-01-14',
    content: '性价比不错，功能齐全，使用体验良好。'
  }
])

// 返回上一页
const goBack = () => {
  router.back()
}

// 分享商品
const shareProduct = () => {
  showToast('分享功能开发中')
}

// 切换收藏状态
const toggleFavorite = () => {
  isFavorite.value = !isFavorite.value
  showToast(isFavorite.value ? '已收藏' : '已取消收藏')
}

// 跳转到购物车
const goToCart = () => {
  router.push({ name: 'Cart' })
}

// 加入购物车
const addToCart = () => {
  if (product.value) {
    cartStore.addToCart(product.value)
    showSuccessToast('已加入购物车')
  }
}

// 立即购买
const buyNow = () => {
  if (product.value) {
    cartStore.addToCart(product.value)
    showToast('跳转到结算页面')
    // 这里可以跳转到结算页面
  }
}

onMounted(() => {
  // 检查商品是否存在
  if (!product.value) {
    showToast('商品不存在')
  }
})
</script>

<style scoped>
.product-detail {
  background: #f8f8f8;
  min-height: 100vh;
  padding-bottom: 60px;
}

.detail-content {
  padding-top: 46px;
}

.product-swipe {
  height: 300px;
  background: white;
}

.product-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.product-info {
  background: white;
  padding: 16px;
  margin-bottom: 10px;
}

.price-section {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.current-price {
  font-size: 24px;
  color: #ff4444;
  font-weight: 600;
}

.product-tag {
  background: #ff4444;
  color: white;
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 4px;
}

.product-title {
  margin: 0 0 8px 0;
  font-size: 18px;
  color: #333;
  line-height: 1.4;
}

.product-subtitle {
  font-size: 14px;
  color: #666;
  line-height: 1.4;
  margin-bottom: 16px;
}

.product-params {
  border-top: 1px solid #f0f0f0;
  margin-top: 16px;
}

.detail-section {
  background: white;
}

.detail-content-tab {
  padding: 16px;
}

.detail-content-tab h3 {
  margin: 20px 0 12px 0;
  font-size: 16px;
  color: #333;
}

.detail-content-tab p {
  margin: 0 0 16px 0;
  font-size: 14px;
  color: #666;
  line-height: 1.6;
}

.detail-images {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.detail-images img {
  width: 100%;
  border-radius: 8px;
}

.reviews-tab {
  padding: 16px;
}

.review-summary {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px;
  background: #f8f8f8;
  border-radius: 8px;
  margin-bottom: 16px;
}

.rating-score {
  font-size: 32px;
  font-weight: 600;
  color: #ff4444;
}

.rating-info {
  flex: 1;
}

.review-count {
  font-size: 12px;
  color: #666;
  margin-top: 4px;
}

.review-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.review-item {
  padding: 16px;
  background: white;
  border-radius: 8px;
}

.review-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.review-user {
  flex: 1;
}

.username {
  font-size: 14px;
  color: #333;
  margin-bottom: 4px;
}

.review-date {
  font-size: 12px;
  color: #999;
}

.review-content {
  font-size: 14px;
  color: #666;
  line-height: 1.6;
}

.action-bar {
  position: fixed;
  bottom: 50px;
  left: 0;
  right: 0;
  background: white;
  padding: 12px 16px;
  border-top: 1px solid #f0f0f0;
  display: flex;
  align-items: center;
  gap: 12px;
  z-index: 100;
}

.action-icons {
  display: flex;
  gap: 20px;
}

.action-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  position: relative;
}

.action-item span {
  font-size: 12px;
  color: #666;
}

.action-buttons {
  flex: 1;
  display: flex;
  gap: 12px;
}

.add-cart-btn,
.buy-now-btn {
  flex: 1;
  height: 44px;
  border-radius: 22px;
}

:deep(.van-tabs__content) {
  background: #f8f8f8;
}

:deep(.van-tab__panel) {
  background: #f8f8f8;
}
</style>